package com.zhehekeji.opsys.api.service.manager;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zhehekeji.opsys.api.code.WhitelistVerifyResultCode;
import com.zhehekeji.opsys.api.common.util.ResultCode;
import com.zhehekeji.opsys.api.entity.TWhitelist;
import com.zhehekeji.opsys.api.entity.TWhitelistUser;
import com.zhehekeji.opsys.api.resp.WhitelistVerifyResp;
import com.zhehekeji.opsys.api.service.CommonService;

/**
 * @ClassName WhitelistManagerService
 * @Description TODO
 * @author chenweiliu chenwei.liu@163.com
 * @date 2017-10-16   上午10:23:11
 */
@Service
public class WhitelistManagerService extends CommonService {
	
	private final static Logger logger = LoggerFactory.getLogger(WhitelistManagerService.class);
	
	public WhitelistVerifyResp verify(String whitelistCode, String userID) {
		WhitelistVerifyResp resp = new WhitelistVerifyResp();
		String resultDesc = String.format("用户[%s]白名单验证成功！", userID);
		if (StringUtils.isBlank(whitelistCode)){
			resultDesc = String.format("用户[%s]白名单验证失败，白名单配置编码非法！", userID);
			logger.error(resultDesc);
			resp.setResultCode(WhitelistVerifyResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc(resultDesc);
			return resp;
		}
		if (StringUtils.isBlank(userID)){
			resultDesc = String.format("用户[%s]白名单验证失败，用户账号非法！", userID);
			logger.error(resultDesc);
			resp.setResultCode(WhitelistVerifyResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc(resultDesc);
			return resp;
		}
		try {
			TWhitelist tmpTWhitelist = entityDao.get(TWhitelist.class, "o.code=?1", new Object[]{whitelistCode});
			if (tmpTWhitelist == null){
				resultDesc = String.format("用户[%s]白名单验证失败，白名单配置[%s]不存在！", userID, whitelistCode);
				logger.error(resultDesc);
				resp.setResultCode(WhitelistVerifyResultCode.WHITELIST_DOES_NOT_EXIST);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			if(tmpTWhitelist.getStatus() == 0){
				resultDesc = String.format("用户[%]白名单验证失败，白名单配置[%s]已停用！", userID, whitelistCode);
				logger.error(resultDesc);
				resp.setResultCode(WhitelistVerifyResultCode.WHITELIST_BEEN_STOPPED);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			Date tmpStartTime = tmpTWhitelist.getStartTime();
			Date tmpEndTime = tmpTWhitelist.getEndTime();
			Date tmpCurrentTime = new Date();
			if (tmpStartTime != null){
				if (tmpCurrentTime.compareTo(tmpStartTime) <0){
					resultDesc = String.format("用户[%s]白名单验证失败，白名单配置[%s]时间还未开始！", userID, whitelistCode);
					logger.error(resultDesc);
					resp.setResultCode(WhitelistVerifyResultCode.WHITELIST_HAS_YET_TO_BEGIN);
					resp.setResultDesc(resultDesc);
					return resp;
				}
			}
			if (tmpEndTime != null){
				if (tmpCurrentTime.compareTo(tmpEndTime) >0){
					resultDesc = String.format("用户[%s]白名单验证失败，白名单配置[%s]时间已结束！", userID, whitelistCode);
					logger.error(resultDesc);
					resp.setResultCode(WhitelistVerifyResultCode.WHITELIST_HAS_COME_TO_AN_END);
					resp.setResultDesc(resultDesc);
					return resp;
				}
			}
			
			TWhitelistUser tmpTWhitelistUser = entityDao.get(TWhitelistUser.class, "o.whitelistId=" + tmpTWhitelist.getId() + " and o.userId=?1",
					new Object[]{userID});
			if (tmpTWhitelistUser == null){
				resultDesc = String.format("用户[%s]白名单验证失败，白名单不存在！", userID);
				logger.error(resultDesc);
				resp.setResultCode(WhitelistVerifyResultCode.WHITELIST_USER_DOES_NOT_EXIST);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			if(tmpTWhitelistUser.getStatus() == 0){
				resultDesc = String.format("用户[%s]白名单验证失败，白名单已停用！", userID);
				logger.error(resultDesc);
				resp.setResultCode(WhitelistVerifyResultCode.WHITELIST_USER_BEEN_STOPPED);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			tmpStartTime = tmpTWhitelistUser.getStartTime();
			tmpEndTime = tmpTWhitelistUser.getEndTime();
			if (tmpStartTime != null){
				if (tmpCurrentTime.compareTo(tmpStartTime) <0){
					resultDesc = String.format("用户[%s]白名单验证失败，白名单还未开始！", userID);
					logger.error(resultDesc);
					resp.setResultCode(WhitelistVerifyResultCode.WHITELIST_USER_HAS_YET_TO_BEGIN);
					resp.setResultDesc(resultDesc);
					return resp;
				}
			}
			if (tmpEndTime != null){
				if (tmpCurrentTime.compareTo(tmpEndTime) >0){
					resultDesc = String.format("用户[%s]白名单验证失败，白名单已结束！", userID);
					logger.error(resultDesc);
					resp.setResultCode(WhitelistVerifyResultCode.WHITELIST_USER_HAS_COME_TO_AN_END);
					resp.setResultDesc(resultDesc);
					return resp;
				}
			}
			
		} catch (Exception e){
			resultDesc = String.format("用户[%s]白名单验证失败，程序异常！", userID);
			logger.error(resultDesc, e);
			resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc(resultDesc);
		}
		logger.info(resultDesc);
		return resp;
	}
}

